class Stack{
    constructor(limit){
        this.limit=limit||30;
        this.undos=[];
        this.redos=[];
        this.saveCommand=null;        //记录保存时undo最后中的命令
    }
    execute(command){
        this.clearRedo();
        command.execute();
        var length=this.undos.length;
        if(length>=this.limit){
             this.undos.shift();
        }
        this.undos.push(command);
    }
    undo(){
        if(this.canUndo()){
            var command=this.undos.pop();
            this.redos.push(command);
            command.undo();
        }
    }
    canUndo(){
        return this.undos.length
    }
    redo(){
        if(this.canRedo()){
            var command=this.redos.pop()
            this.undos.push(command);
            command.redo();
        }
    }
    canRedo(){
        return this.redos.length
    }
    save(){
        this.saveCommand==this.undos[this.undos.length-1];
    }
    dirty(){
        return this.saveCommand!=this.undos[this.undos.length-1];
    }
    clearRedo(){
        this.redos=[];
    }
}

class Command{
    constructor(name){
        this.name=name;
    }
    execute(){}
    undo(){}
    redo(){
        this.execute();
    }
}

export{
    Stack,
    Command
}
